//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace Microsoft.Research.DataLayer
{
    /// <summary>
    /// Partial class implementation of JobSchedule.
    /// </summary>
    public partial class JobSchedule
    {
        #region CommonSearches
        /// <summary>
        /// Common Searches provided.
        /// </summary>
        public sealed class CommonSearches
        {
            /// <summary>
            /// Prevents a default instance of the <see cref="CommonSearches"/> class from being created.
            /// </summary>
            private CommonSearches()
            {
            }

            /// <summary>
            /// Gets the jobs pending for schedule.
            /// </summary>            
            /// <param name="connection">The registry handle.</param>
            /// <returns></returns>
            public static Collection<JobSchedule> GetPendingSchedules(Connection connection)
            {
                if (null == connection)
                {
                    throw new ArgumentNullException("connection");
                }

                Collection<JobSchedule> schedules = new Collection<JobSchedule>();
                try
                {
                    JobSchedule.ISearch searchQuery = JobSchedule.ISearch.Create();
                    searchQuery.Query = JobSchedule.ISearch.AND(
                            JobSchedule.ISearch.Status(EnumFieldBase.Condition.NotEquals, ScheduleStatus.Scheduled),
                            JobSchedule.ISearch.Status(EnumFieldBase.Condition.NotEquals, ScheduleStatus.Scheduling));
                    schedules = new Collection<JobSchedule>(JobSchedule.Search(searchQuery, connection));
                }
                catch (RecordNotFoundException)
                {
                }
                return schedules;
            }

            /// <summary>
            /// Gets all scheduled jobs.
            /// </summary>
            /// <param name="connection">The registry handle.</param>
            /// <returns></returns>
            public static Collection<JobSchedule> GetAllScheduledJobs(Connection connection, JobSchedule.ISearch.ISearchClause filter)
            {
                if (null == connection)
                {
                    throw new ArgumentNullException("connection");
                }

                Collection<JobSchedule> schedules = null;

                try
                {
                    JobSchedule.ISearch searchQuery = JobSchedule.ISearch.Create();
                    if (null == filter)
                    {
                        searchQuery.Query = JobSchedule.ISearch.Status(EnumFieldBase.Condition.NotEquals, ScheduleStatus.Cancelled);
                    }
                    else
                    {
                        searchQuery.Query = JobSchedule.ISearch.AND(
                                                JobSchedule.ISearch.Status(EnumFieldBase.Condition.NotEquals, ScheduleStatus.Cancelled),
                                                filter);
                    }

                    schedules = new Collection<JobSchedule>(JobSchedule.Search(searchQuery, connection));


                }
                catch (RecordNotFoundException)
                {
                }
                return schedules;
            }
        }
        #endregion
    }
}
